﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhsystem.Main;
using dhSystemBLL.produceBLL;
using baseSet.importExport;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.produceDataSetTableAdapters;
using FastReport;
using dhDataAcess.baseBLL;
using baseSet.power;

namespace dhsystem.produceForm
{
    public partial class FrmpackStockInMaster : Form
    {
        packStockInBLL packStockIn = new packStockInBLL();
        string state;
        string stockName;
        public FrmpackStockInMaster(string _stockName)
        {
            InitializeComponent();
            stockName = _stockName;
        }
        //打开窗口时显示列表数据
        private void FrmpackStockInMaster_Load(object sender, EventArgs e)
        {
            getData();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
            state = "新增";
            FrmpackStockInMasterAdd packStockInMasterAdd = new FrmpackStockInMasterAdd(bindingSourcepackStockInMaster, state,stockName );
            packStockInMasterAdd.ShowDialog();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {

            FrmDataQuery query = new FrmDataQuery(moduleInfo.packStockInMaster );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
                dGridpackStockInDetail.DataSource = query.rTable;
            }
        }
        //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
            state = "编辑";
            FrmpackStockInMasterAdd packStockInMasterAdd = new FrmpackStockInMasterAdd(bindingSourcepackStockInMaster, state,stockName );
            packStockInMasterAdd.ShowDialog();
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
            string billNo = dGridpackStockInMaster.Rows[dGridpackStockInMaster.CurrentRow.Index].Cells[1].Value.ToString();
            try
            {
                DialogResult result = MessageBox.Show("是否删除：" + billNo + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourcepackStockInMaster.DataSource = packStockIn.packStockInMasterDel(billNo);

                }
                getData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {

            try
            {
                bindingSourcepackStockInDetail.DataSource = packStockIn.getspackStockInDetailAllBybillNo("");
                dGridpackStockInDetail.DataSource = bindingSourcepackStockInDetail;
                DcolumnSet.colSet(moduleInfo.packStockInDetail, dGridpackStockInDetail);
                baseSet.Query q = new baseSet.Query(null, dhDataAcess.baseBLL.dataConn.connOpen());
                bindingSourcepackStockInMaster.DataSource = q.sqlQueryLikeAllToTable(moduleInfo.packStockInMaster  , textbillNoQuery.Text.ToString().Trim());
                dGridpackStockInMaster.DataSource = bindingSourcepackStockInMaster;
                //DcolumnSet.colSet(moduleInfo.productList, dGridProductList);
                DcolumnSet.colSet(moduleInfo.packStockInMaster, dGridpackStockInMaster);
                //string billNo=null;
                //int i=0;
                //dhDataAcess.dataAcess.produceDataSet.packStockInDetailDataTable ptable = (dhDataAcess.dataAcess.produceDataSet.packStockInDetailDataTable)bindingSourcepackStockInDetail.DataSource;
                //foreach (dhDataAcess.dataAcess.produceDataSet.packStockInMasterRow row in ptable.Rows )
                //{
                //    if (i == 0)
                //    {
                //        billNo = row.billNo;
                //        i++;
                //    }
                //}
                //bindingSourcepackStockInDetail.DataSource = packStockIn.getspackStockInDetailAllBybillNo(billNo);
                //dGridpackStockInDetail.DataSource = bindingSourcepackStockInDetail;
                //DcolumnSet.colSet(moduleInfo.packStockInDetail , dGridpackStockInDetail);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
            getData();
        }
        //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //datagridview第一列显示行号
        private void dGridpackStockInMaster_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
        //datagridview第一列显示行号
        private void dGridpackStockInDetail_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);

          
        }
        private void getData()
        {
            packStockInDetailTableAdapter packStockInDetailAdapter = new packStockInDetailTableAdapter();
            packStockInMasterTableAdapter packStockInMasterAdapter = new packStockInMasterTableAdapter();
            produceDataSet packStockIn = new produceDataSet();
            packStockInDetailAdapter.FillpackStockInDetailALL (packStockIn.packStockInDetail);
            packStockInMasterAdapter.FillpackStockInMasterALL(packStockIn.packStockInMaster);
            bindingSourcepackStockInMaster.DataSource = packStockIn;
            bindingSourcepackStockInMaster.DataMember = "packStockInMaster";
            dGridpackStockInMaster.DataSource = bindingSourcepackStockInMaster;

            DcolumnSet.colSet(moduleInfo.packStockInMaster, dGridpackStockInMaster);
            toolStripStatusLabel1.Text = "领料主表数据总条数:" + dGridpackStockInMaster.RowCount.ToString() + "条";
            bindingSourcepackStockInDetail.DataSource = bindingSourcepackStockInMaster;

            bindingSourcepackStockInDetail.DataMember = "fk_packStockInDetail_billNo";
            dGridpackStockInDetail.DataSource = bindingSourcepackStockInDetail;
            DcolumnSet.colSet(moduleInfo.packStockInDetail, dGridpackStockInDetail);
            toolStripStatusLabel3.Text = "领料从表数据总条数:" + dGridpackStockInDetail.RowCount.ToString() + "条";

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.packStockInMaster );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }

        private void toolExport_Click(object sender, EventArgs e)
        {
            FrmExcelExport frm = new FrmExcelExport(moduleInfo.packStockInMaster , dGridpackStockInMaster  , "包装入库信息");
            frm.ShowDialog();
        }

        private void 普通客户打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridpackStockInMaster.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(packStockIn.getV_packbiaoqianAllBybillNo(billNo));
          // r.Load(@"E:\东华软件项目\报表\packbiaoqian.frx");
           r.Load(@"\\192.168.0.27\东华软件\报表\packbiaoqian.frx");
            r.RegisterData(ds);
          // r.Design();
           r.Show();
        }

        private void 河源ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string billNo = dGridpackStockInMaster.CurrentRow.Cells["billNo"].Value.ToString();
            Report r = new Report();
            DataSet ds = new DataSet();
            ds.Tables.Add(packStockIn.getV_packbiaoqianAllBybillNo(billNo));
            //r.Load(@"E:\东华软件项目\报表\packbiaoqianheyuan.frx");
            r.Load(@"\\192.168.0.27\东华软件\报表\packbiaoqianheyuan.frx");
            r.RegisterData(ds);
         // r.Design();
           r.Show();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            foreach (DataGridViewRow row in dGridpackStockInDetail.SelectedRows)
            {
                list.Add(row.Cells["boxno"].Value.ToString());
            }
            basicSql.getproduceNo((list),dGridpackStockInDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString());
            Report r = new Report();
            DataTable V_orderproduceTable = new DataTable();
            V_orderproduceTable.Load(basicSql.getproduceNo((list), dGridpackStockInDetail.CurrentRow.Cells["orderDetailNo"].Value.ToString()));
            DataSet ds = new DataSet();
            ds.Tables.Add(V_orderproduceTable);
            //  ds.Tables.Add(orderProduceList.getproduceByproduceNo(dGridorderProduceList.CurrentRow.Cells["produceNo"].Value.ToString()));
            //  r.Load(@"e:\东华软件项目\报表\orderproduce.frx");
            r.Load(@"\\192.168.0.27\东华软件\报表\packbiaoqian.frx");

            r.RegisterData(ds);
            r.Design();
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {

        }

        private void dGridpackStockInMaster_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            string billNo = dGridpackStockInMaster.Rows[dGridpackStockInMaster.CurrentRow.Index].Cells[1].Value.ToString();
            bindingSourcepackStockInDetail.DataSource = packStockIn.getspackStockInDetailAllBybillNo(billNo);
            dGridpackStockInDetail.DataSource = bindingSourcepackStockInDetail;
            DcolumnSet.colSet(moduleInfo.packStockInDetail, dGridpackStockInDetail);
        }

        private void buttImport_Click(object sender, EventArgs e)
        {

        }

        private void dGridpackStockInMaster_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string billNo = dGridpackStockInMaster.Rows[dGridpackStockInMaster.CurrentRow.Index].Cells[1].Value.ToString();
            bindingSourcepackStockInDetail.DataSource = packStockIn.getspackStockInDetailAllBybillNo(billNo);
            dGridpackStockInDetail.DataSource = bindingSourcepackStockInDetail;
            DcolumnSet.colSet(moduleInfo.packStockInDetail, dGridpackStockInDetail);
        }
    }
}
